<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of myphoto
 * 
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Myphoto extends MY_Controller {

    //put your code here


    function myphoto() {
        parent :: __construct();
        $this->load->model('pickup_model');
        $this->load->model('location_model');
        $this->load->model('member_model');
        $this->load->model('management_model');
        $this->load->model('listing_model');
        $this->load->model('terms_model');
    }

//--Delete Photo ---//
    function delete($id,$listing_id){
        if($id){
            $term['data'] = $this->terms_model->get_term($id);
            $fname = "./assets/images/data/photo/".$term['data']->description;
            unlink($fname);
            $sql = "delete from terms where id = $id";
            $query = $this->db->query($sql);
        }
        redirect('member/myphoto/photo/'.$listing_id);
    }

//--photos (Subha)---//    
    function photo($listing_id) {
        $id = intval($this->session->userdata('mem_id'));
        $data['member']['info'] = $this->member_model->get_member($id);
        $data['listing']['info'] = $this->listing_model->get_listing($listing_id);
        $data['terms']['listing-photo'] = $this->terms_model->get_terms('listing-photo',$listing_id);

        $this->template->title('Welcome', 'carz.com')
                ->set_partial('header', 'header/header')
                ->set_partial('footer', 'footer/footer')
                ->set_partial('customer_care', 'sidebar/customer_care')
                ->set_partial('social_share', 'sidebar/social_share')
                ->set_partial('search_reviews', 'sidebar/search_reviews')
                ->set_partial('recent_news', 'sidebar/recent_news')
                ->set_partial('recent_reviews', 'sidebar/recent_reviews')
                ->build('pages/member/photo', $data);
    }

    //-- Upload photo --//
    function upload($listing_id) {
        if ($_FILES['userfile']['error'] == 0) {
            $ext = preg_replace("/.*\.([^.]+)$/", "\\1", $_FILES['userfile']['name']);
            $filename = md5(date('dmyhms', now())) . "." . $ext;
            $config['file_name'] = $filename;
            $config['upload_path'] = './assets/images/data/photo/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['remove_spaces'] = true;
            $this->load->library('upload', $config);
            if ($this->upload->do_upload('userfile')) {
                $this->session->set_flashdata('message', 'Photo Uploaded successfully !');
                $this->_insert_photo_file($filename,$listing_id);
                redirect('member/myphoto/photo/' . $listing_id);
            }
        } else {
            $this->session->set_flashdata('message', $this->upload->display_errors('<p class="error">', '</p>'));
            redirect('member/myphoto/photo/' . $listing_id);
        }
    }

    private function _insert_photo_file($filename='', $listing_id) {
        $insert_data = array(
            'listing_id' => intval($listing_id),
            'term' => 'listing-photo',
            'description' => trim($filename)
        );
        $this->db->insert('terms', $insert_data);
    }

}

?>
